/**
 * Created by zdx  on 2016/8/26.
 *
 *
 */

define(['controller'], function (c) {
   /* AMap.Marker.prototype.CrTag = function (visible,map,te) {
        var html='<div><div class="amap-info-content amap-info-outer">'+text+'</div><a class="amap-info-close" href="javascript: void(0)" style="right: 5px;">×</a><div class="amap-info-sharp" style="height: 23px;"></div></div>'
        if (visible) {
            if (!this.tag ) {
                c.conLog(this.getPosition())
                this.tag = new AMap.Marker({
                    content:"333333",
                    position: this.getPosition(),
                    //offset: new AMap.Pixel(-100, 20),
                    zIndex: '101',
                    map:map.map
                });
            }else{
                this.tag.setPosition(this.getPosition())
            }
        }
        else {
            if (this.tag) {
                this.tag.setMap(null);
                this.tag = null;
            }
        }
    };*/



    var map =function () {
        this.map=null;
        this.rightLngLat=null;

    };
    map.prototype.init=function (dom,op) {

        var newOP=$.extend({resizeEnable: false,center:[104.2272944, 30.5729027],zoom:13},op);
        // 高德地图API功能
        this.map = new AMap.Map(dom, newOP);    // 创建Map实例
    };
    map.prototype.setMapType=function () {
       /* var type= new AMap.MapType({
            defaultType:0 //使用2D地图
        });
        this.map.addControl(type);*/
       var _this=this;
       var Satellite = new AMap.TileLayer.Satellite({zIndex:10});

        return {
           show:function () {
               Satellite.setMap(_this.map);
           },
            hide:function () {
                Satellite.setMap(null);
            }
        };
    };
    map.prototype.setMapControl=function () {

        this.map.addControl(new AMap.ToolBar());

        this.map.addControl(new AMap.Scale());

        this.map.addControl(new AMap.OverView({isOpen:true}));
    };
    map.prototype.addEventListener=function (events,fn) {
        AMap.event.addListener(this.map,events,function (e) {
            fn(e.lnglat)
        });
    };
    map.prototype.createMark=function (op) {
        return new Marker(op);
    };
    map.prototype.createPolyline=function (path,op) {
        return new Polyline(path,op);
    };
    map.prototype.createPolygon=function (path,op) {
        return new Polygon(path,op);
    };
    map.prototype.createCircle=function (path,op) {
        return new Circle(path,op);
    };
    map.prototype.createContextMenu=function () {
        return new ContextMenu();
    };
    map.prototype.createInfoWindow=function (content,opt) {
        return new InfoWindow(content,opt);
    };
    map.prototype.createRangingTool=function (opt) {
        return new RangingTool(this.map,opt);
    };


    map.prototype.addContextMenu=function (menu) {
        var _this=this;
       _this.map.on('rightclick', function(e) {
            _this.rightLngLat=e.lnglat;
            menu.mk.open(_this.map, e.lnglat);
        });
    };
    map.prototype.getZoom=function () {
        return this.map.getZoom()
    };
    map.prototype.getCenter=function () {
        return this.map.getCenter();
    };
    map.prototype.setFitView=function (op) {
        return this.map.setFitView(op);
    };
    map.prototype.setCenter=function (lnglat) {
        return this.map.setCenter(lnglat);
    };
    map.prototype.setZoom=function (num) {
        return this.map.setZoom(num);
    };

    map.prototype.addOverlay=function(Marker){
        Marker.mk.setMap(this.map);
    };
    map.prototype.remove=function(Markers){
        var mks=[];
        $.each(Markers,function (i,marker) {
            mks.push(marker.mk);
        });
        this.map.remove(mks)
    };


    map.prototype.getWeather=function(e,callback){

        //查询实时天气信息, 查询的城市到行政级别的城市，
        var geocoder = new AMap.Geocoder();
        geocoder.getAddress([e.lng,e.lat],function (status,result) {
            if (status == 'complete') {
                var adcode = result.regeocode.addressComponent.adcode;
                var weather = new AMap.Weather();
                weather.getLive(adcode, function (err, data) {
                    if (!err) {
                        callback(data)
                    }
                });
            }
        });

    };

    map.prototype.getAddress=function(e,callback){

        // 查询的城市到行政级别的城市，
        var geocoder = new AMap.Geocoder();
        geocoder.getAddress([e.lng,e.lat],function (status,result) {
            if (status == 'complete') {
                var addressComponent = result.regeocode.addressComponent;
                callback(addressComponent);
            }
        });

    };

    function Marker (op) {//覆盖物对象
        var newOP=$.extend({id:'marker0',position: [op.lnglat.lng, op.lnglat.lat]},op);
        this.mk= new AMap.Marker(newOP);
        this.id=op.id;
    }
    Marker.prototype.setIcon=function (url,size) {
        var asize=new AMap.Size(size[0],size[1]);
        var myIcon =new AMap.Icon({
            image:url,
            size:asize,
            imageSize:asize
        });
        this.mk.setOffset(new AMap.Pixel(-size[0]/2,-size[1]/2));
        this.mk.setIcon(myIcon);
    };

    Marker.prototype.setPosition=function (lnglat) {
        var Alnglat=new AMap.LngLat(lnglat.lng,lnglat.lat);
        this.mk.setPosition(Alnglat);
        if(this.tag){
            this.tag.setPosition(Alnglat);
        }

    };
    Marker.prototype.getPosition=function () {
        return this.mk.getPosition();
    };
    Marker.prototype.setLabel=function (op) {
        this.mk.setLabel(op);
    };
    Marker.prototype.setRotation=function (num) {
        this.mk.setAngle(num);

    };
    Marker.prototype.hide=function () {
        this.mk.hide();
        this.hideInfoWindow()
    };
    Marker.prototype.show=function () {
        this.mk.show();
        this.showInfoWindow()
    };
    Marker.prototype.createInfoWindow=function (map,html) {

        this.tag = new AMap.Marker({
            content:html,
            position: this.getPosition(),
            offset: new AMap.Pixel(-100, 20),
            zIndex: '101',
            visible:false,
            map:map.map
        });
    };
    Marker.prototype.isShowInfoWindow=function () {
        if(this.tag){
            return this.tag.get
        }
        return false
    };
    Marker.prototype.hideInfoWindow=function () {
        if(this.tag){
            this.tag.hide();
        }
    };
    Marker.prototype.showInfoWindow=function () {
        if(this.tag){
            this.tag.show();
        }
    };
    Marker.prototype.setIWcontent=function (html) {
        if(this.tag){
            this.tag.setContent(html);
        }
    };
    Marker.prototype.on=function (events,fn) {
        this.mk.on(events,function (e) {
            fn(e.lnglat)
        });
    };


    var pathFormat=function (path) {//路径格式化
        var _path=[];
        $.each(path,function (i,point) {
            _path.push([point.lng, point.lat]);
        });
        return _path;
    };
    /*
    *  path :[{lng:xx,lat:xx}]
    * */
    function Polyline(path,op) {//画线对象
        var newOP=$.extend({path: pathFormat(path)},op);
        this.mk=new AMap.Polyline(newOP);
    }

    Polyline.prototype.setPath=function (path) {
        this.mk.setPath(pathFormat(path));
    };
    Polyline.prototype.hide=function () {
        this.mk.hide();
    };
    Polyline.prototype.show=function () {
        this.mk.show();
    };

    Polyline.prototype.on=function (events,fn) {
        this.mk.on(events,function (e) {
            fn(e.lnglat)
        });
    };

    function Polygon(path,op) {//画面对象
        var newOP=$.extend({path: pathFormat(path)},op);
        this.mk=new AMap.Polygon(newOP);
    }
    Polygon.prototype=Polyline.prototype;


    function Circle(path,op) {//画圆对象
        Polyline.call(this, arguments);
        var newOP=$.extend({center: new AMap.LngLat(path[0].lng, path[0].lat)},op);
        this.mk=new AMap.Circle(newOP);
    }
    Circle.prototype=Polyline.prototype;
    Circle.prototype.setRadius=function (radius) {
        this.mk.setRadius(radius);
    };
    Circle.prototype.getRadius=function () {
        return this.mk.getRadius();
    };
    Circle.prototype.setCenter=function (path) {
        this.mk.setCenter(new AMap.LngLat(path[0].lng, path[0].lat));
    };



    function ContextMenu() {//右键菜单
        this.mk=new AMap.ContextMenu();
    }

    ContextMenu.prototype.addItems=function (items) {
        for(var i=0; i < items.length; i++){
            this.mk.addItem(items[i].text,items[i].callback);
        }
    };

    function InfoWindow(content,opt) {
        var newOP=$.extend({content:content},opt);
        this.mk=new AMap.InfoWindow(newOP);
    }
    InfoWindow.prototype.open=function (map,lnglat) {
        this.mk.open(map.map,lnglat)
    };
    InfoWindow.prototype.setContent=function (content) {
        this.mk.setContent(content)
    };
    InfoWindow.prototype.setPosition=function (lnglat) {
        this.mk.setPosition(new AMap.LngLat(lnglat.lng,lnglat.lat));
    };
    InfoWindow.prototype.getIsOpen=function () {
        return this.mk.getIsOpen();
    };


    function RangingTool(map,opt) {
        var newOP=$.extend({},opt);
        this.mk=new AMap.RangingTool(map);
    }
    RangingTool.prototype.turnOn=function () {
        this.mk.turnOn()
    };
    RangingTool.prototype.turnOff=function () {
        this.mk.turnOff()
    };

    return map;
});